import { UserInfo, ILogin } from '@/types'
import { createSlice, createAsyncThunk, PayloadAction } from '@reduxjs/toolkit'
const initUser: UserInfo = {
  name: 'asdnet',
  account: 'hytime',
  token: '',
  id: '001'
}
export const userLogin = createAsyncThunk(
  'user/login',
  async (userInput: ILogin) => {
    const userInfo: UserInfo = {}
    await Object.assign(userInfo, initUser)

    return userInfo
  }
)
export const userSlice = createSlice({
  name: 'user',
  initialState: {
    value: initUser
  },
  reducers: {
    userLogout: (state, { payload = null }) => {
      return (state.value = payload)
    }
  },
  //异步
  extraReducers(builder) {
    builder
      .addCase(userLogin.fulfilled, (state, actions) => {
        state.value = actions.payload
        console.log('fulfilled')
      })
      .addCase(userLogin.pending, (state, actions) => {
        console.log('pending')
      })
      .addCase(userLogin.rejected, (state, actions) => {
        console.log('rejected')
      })
  }
})
export default userSlice.reducer
